<?php 
class ModelProductAttributeGroup extends Model {
	public function addAttributeGroup($data) {
		$name = $this->db->escape($data['name']);
		$this->db->query("INSERT INTO " . DB_PREFIX . "attribute_group SET `name` = '" . $name . "'");
		$attribute_group_id = $this->db->getLastId();
		
		foreach ($data['attribute_group_description'] as $language_id => $value) {
			$sql = "INSERT INTO " . DB_PREFIX . "attribute_group_description SET attribute_group_id = '" . (int)$attribute_group_id . 
				"', language_id = '" . (int)$language_id . "', display_name = '";
			if($value['display_name']){
				$sql .= $this->db->escape($value['display_name']) . "'";
			}else{
				$sql .= $name . "'";
			}
			$this->db->query($sql);
		}
	}

	public function editAttributeGroup($attribute_group_id, $data) {
		$name = $this->db->escape($data['name']);
		$this->db->query("UPDATE " . DB_PREFIX . "attribute_group SET `name` = '" . $name . 
			"' WHERE attribute_group_id = '" . (int)$attribute_group_id . "'");
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "attribute_group_description WHERE attribute_group_id = '" . (int)$attribute_group_id . "'");
		foreach ($data['attribute_group_description'] as $language_id => $value) {
			$sql = "INSERT INTO " . DB_PREFIX . "attribute_group_description SET attribute_group_id = '" . (int)$attribute_group_id . 
				"', language_id = '" . (int)$language_id . "', display_name = '";
			if($value['display_name']){
				$sql .= $this->db->escape($value['display_name']) . "'";
			}else{
				$sql .= $name . "'";
			}
			$this->db->query($sql);
		}
	}
		
	public function getAttributeGroup($attribute_group_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "attribute_group WHERE attribute_group_id = '" . (int)$attribute_group_id . "'");
		return $query->row;
	}
	
	public function getAttributeGroupDescriptions($attribute_group_id) {
		$attribute_group_data = array();
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "attribute_group_description WHERE attribute_group_id = '" . (int)$attribute_group_id . "'");
		foreach ($query->rows as $result) {
			$attribute_group_data[$result['language_id']] = array(
				'attribute_group_id' => $result['attribute_group_id'],
				'display_name' => $result['display_name']
			);
		}
		return $attribute_group_data;
	}
		
	public function getAttributeGroups($data = array()) {
		$sql = "SELECT ag.attribute_group_id, ag.`name`, agd.display_name FROM " . DB_PREFIX . "attribute_group ag LEFT JOIN " . 
			DB_PREFIX . "attribute_group_description agd ON (ag.attribute_group_id = agd.attribute_group_id AND agd.language_id = '" . 
			(int)$this->config->get('config_language_id') . "')";
		if (!empty($data['filter_display_name'])) {
			$sql .= " WHERE LCASE(agd.display_name) LIKE '" . $this->db->escape($data['filter_display_name']) . "%'";
		}
		$sql .= " ORDER BY agd.display_name ASC";
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}				
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}	
		$query = $this->db->query($sql);
		return $query->rows;
	}
	
	public function getTotalAttributeGroups($data = array()) {
		$sql = "SELECT COUNT(*) AS total FROM " . DB_PREFIX . "attribute_group ag LEFT JOIN " . 
			DB_PREFIX . "attribute_group_description agd ON (ag.attribute_group_id = agd.attribute_group_id AND agd.language_id = '" . 
			(int)$this->config->get('config_language_id') . "')";
		if (!empty($data['filter_display_name'])) {
			$sql .= " WHERE LCASE(agd.display_name) LIKE '" . $this->db->escape($data['filter_display_name']) . "%'";
		}
      	$query = $this->db->query($sql);
		return $query->row['total'];
	}	
}
?>